package com.example.shuiyin.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${minio.endpoint:http://localhost:9000}")
    private String minioEndpoint;
    
    @Value("${minio.bucketName:shuiyin}")
    private String minioBucket;
    
    @Value("${storage.local.path:uploads}")
    private String localStoragePath;
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置本地文件资源映射
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations("file:" + localStoragePath + "/");
        
        // 配置MinIO资源访问URL转发
        registry.addResourceHandler("/api/avatar/**")
                .addResourceLocations(minioEndpoint + "/" + minioBucket + "/avatars/");
    }
    
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .maxAge(3600);
    }
} 